package org.niux.permissionComponent;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

import org.niux.permissionComponent.exception.MyException;

/**
 * 
 * Copyright (c) 2013,niux<br>
 * All rights reserved.<br>
 * 
 * 文件名称：Auth.java<br>
 * 摘    要：权限组件入口类<br>
 * <br>
 * 当前版本：<br>
 * 作    者：梁铭显 Email: <a href="mailto:liangmingxian@gmail.com">liangmingxian@gmail.com</a><br>
 * 完成日期：2013-9-26<br>
 * <br>
 * 取代版本：<br>
 * 原作者  ：<br>
 * 完成日期：2013-9-26<br>
 */
public class Auth {
	private String url;
	
	/**
	 * 
	 * 方法概述：发送登录请求
	 * 描述：发送登录请求
	 * @param name
	 * @param password
	 * @param app
	 * @return
	 * @throws MyException
	 * 返回类型：String
	 * 修改人：梁铭显 
	 * 修改时间：2013-9-26
	 */
	public String login(String name,String password,String app) throws MyException {
		StringBuffer params = new StringBuffer();
		try {
			//构建查询参数
			params.append("action=" + URLEncoder.encode("login", Constant.REQUEST_ENCODING) + "&");
			params.append("name=" + URLEncoder.encode(name, Constant.REQUEST_ENCODING) + "&");
			params.append("password=" + URLEncoder.encode(password, Constant.REQUEST_ENCODING) + "&");
			params.append("app=" + URLEncoder.encode(app, Constant.REQUEST_ENCODING) + "&");
			params = params.deleteCharAt(params.length() - 1);
			
			String responseContent = connectAims(params.toString());
			
            return responseContent;
		} catch (UnsupportedEncodingException e) {
			MyException myException = new MyException(Integer.toString(Constant.ENCODING_EXCEPTION));
			throw myException;
		}
	}
	
	/**
	 * 
	 * 方法概述：获取权限代码
	 * 描述：获取权限代码
	 * @param accountId
	 * @return
	 * @throws MyException
	 * 返回类型：String
	 * 修改人：梁铭显 
	 * 修改时间：2013-9-26
	 */
	public String getAuths(String accountId) throws MyException {
		StringBuffer params = new StringBuffer();
		try {
			//构建查询参数
			params.append("action=" + URLEncoder.encode("getAuth", Constant.REQUEST_ENCODING) + "&");
			params.append("accountid=" + URLEncoder.encode(accountId, Constant.REQUEST_ENCODING) + "&");
			params = params.deleteCharAt(params.length() - 1);
			
			String responseContent = connectAims(params.toString());
			
            return responseContent;
		} catch (UnsupportedEncodingException e) {
			MyException myException = new MyException(Integer.toString(Constant.ENCODING_EXCEPTION));
			throw myException;
		}
	}
	
	/**
	 * 
	 * 方法概述：新增账户
	 * 描述：新增账户
	 * @param name
	 * @param password
	 * @param app
	 * @return
	 * @throws MyException
	 * 返回类型：String
	 * 修改人：梁铭显 
	 * 修改时间：2013-9-26
	 */
	public String addAccount(String name,String password,String app) throws MyException {
		StringBuffer params = new StringBuffer();
		try {
			//构建查询参数
			params.append("action=" + URLEncoder.encode("addAccount", Constant.REQUEST_ENCODING) + "&");
			params.append("name=" + URLEncoder.encode(name, Constant.REQUEST_ENCODING) + "&");
			params.append("password=" + URLEncoder.encode(password, Constant.REQUEST_ENCODING) + "&");
			params.append("app=" + URLEncoder.encode(app, Constant.REQUEST_ENCODING) + "&");
			params = params.deleteCharAt(params.length() - 1);
			
			String responseContent = connectAims(params.toString());
			
            return responseContent;
		} catch (UnsupportedEncodingException e) {
			MyException myException = new MyException(Integer.toString(Constant.ENCODING_EXCEPTION));
			throw myException;
		}
	}
	
	/**
	 * 
	 * 方法概述：删除账户
	 * 描述：删除账户
	 * @param name
	 * @param password
	 * @param app
	 * @return
	 * @throws MyException
	 * 返回类型：String
	 * 修改人：梁铭显 
	 * 修改时间：2013-9-26
	 */
	public String removeAccount(String app,String name) throws MyException {
		StringBuffer params = new StringBuffer(); 
		try {
			//构建查询参数
			params.append("action=" + URLEncoder.encode("removeAccount", Constant.REQUEST_ENCODING) + "&");
			params.append("name=" + URLEncoder.encode(name, Constant.REQUEST_ENCODING) + "&");
			params.append("app=" + URLEncoder.encode(app, Constant.REQUEST_ENCODING) + "&");
			params = params.deleteCharAt(params.length() - 1);
			
			String responseContent = connectAims(params.toString());
			
            return responseContent;
		} catch (UnsupportedEncodingException e) {
			MyException myException = new MyException(Integer.toString(Constant.ENCODING_EXCEPTION));
			throw myException;
		}
	}
	
	/**
	 * 
	 * 方法概述：修改密码
	 * 描述：修改密码
	 * @param accountId
	 * @param password
	 * @return
	 * @throws MyException
	 * 返回类型：String
	 * 修改人：梁铭显 
	 * 修改时间：2013-9-26
	 */
	public String updatePassword(String accountId,String password) throws MyException {
		StringBuffer params = new StringBuffer();
		try {
			//构建查询参数
			params.append("action=" + URLEncoder.encode("updatePassword", Constant.REQUEST_ENCODING) + "&");
			params.append("accountid=" + URLEncoder.encode(accountId, Constant.REQUEST_ENCODING) + "&");
			params.append("password=" + URLEncoder.encode(password, Constant.REQUEST_ENCODING) + "&");
			params = params.deleteCharAt(params.length() - 1);
			
			String responseContent = connectAims(params.toString());
			
            return responseContent;
		} catch (UnsupportedEncodingException e) {
			MyException myException = new MyException(Integer.toString(Constant.ENCODING_EXCEPTION));
			throw myException;
		}
	}
	
	/**
	 * 
	 * 方法概述：向权限系统发起请求
	 * 描述：向权限系统发起请求
	 * @param params
	 * @return
	 * @throws MyException
	 * 返回类型：String
	 * 修改人：梁铭显 
	 * 修改时间：2013-9-26
	 */
	public String connectAims(String params) throws MyException{
		try {
			URL requestUrl = new URL(url);
			
			HttpURLConnection conn = (HttpURLConnection)requestUrl.openConnection();
			conn.setRequestMethod("GET");
			
			conn.setConnectTimeout(Constant.CONNECT_TIME_OUT);
			conn.setReadTimeout(Constant.READ_TIME_OUT);
			
			conn.setDoOutput(true);
			
			byte[] b = params.toString().getBytes();
			
			//发送请求
			conn.getOutputStream().write(b, 0, b.length);
			conn.getOutputStream().flush();
			conn.getOutputStream().close();
			
			//接收返回数据
			InputStream in = conn.getInputStream();
			BufferedReader read = new BufferedReader(new InputStreamReader(in,Constant.REQUEST_ENCODING));
			String tempLine = read.readLine();
			StringBuffer temp = new StringBuffer();
			String separator = System.getProperty("line.separator");
			
			while (tempLine != null){
                temp.append(tempLine);
                temp.append(separator);
                tempLine = read.readLine();
            }
			
			String responseContent = temp.toString();
			
			read.close();
            in.close();
			
            return responseContent;
		} catch (MalformedURLException e) {
			MyException myException = new MyException(Integer.toString(Constant.URL_FORMAT_EXCEPTION));
			throw myException;
		} catch (IOException e) {
			MyException myException = new MyException(Integer.toString(Constant.NETWORK_EXCEPTION));
			throw myException;
		}
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}
	
	public static void main(String[] args) {
		Auth auth = new Auth();
		auth.setUrl("http://localhost:8080/aims/serverListener");
		
		String rs = "";
		try {
//			rs = auth.login("admin", "admin", "1");
//			System.out.println(rs);
//			rs = auth.getAuths("1");
//			System.out.println(rs);
//			rs = auth.addAccount("niux", "123456", "1");
//			System.out.println(rs);
			rs = auth.updatePassword("2", "admin");
			System.out.println(rs);
		} catch (MyException e) {
			e.printStackTrace();
		}
	}
}
